package coop.main.web;
import coop.main.domain.Detalle;
import coop.main.domain.Maestro;
import coop.main.service.MaestroService;
import coop.main.web.util.MessageFactory;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.el.ELContext;
import javax.el.ExpressionFactory;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.component.html.HtmlOutputText;
import javax.faces.component.html.HtmlPanelGrid;
import javax.faces.context.FacesContext;
import org.primefaces.component.inputtext.InputText;
import org.primefaces.component.message.Message;
import org.primefaces.component.outputlabel.OutputLabel;
import org.primefaces.context.RequestContext;
import org.primefaces.event.CloseEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.roo.addon.jsf.managedbean.RooJsfManagedBean;
import org.springframework.roo.addon.serializable.RooSerializable;

@Configurable
@ManagedBean(name = "maestroBean")
@SessionScoped
@RooSerializable
@RooJsfManagedBean(entity = Maestro.class, beanName = "maestroBean")
public class MaestroBean implements Serializable {

	@Autowired
    MaestroService maestroService;

	private String name = "Maestroes";

	private Maestro maestro;

	private List<Maestro> allMaestroes;

	private boolean dataVisible = false;

	private List<String> columns;

	private HtmlPanelGrid createPanelGrid;

	private HtmlPanelGrid editPanelGrid;

	private HtmlPanelGrid viewPanelGrid;

	private boolean createDialogVisible = false;

	private List<Detalle> selectedDetalles;

	@PostConstruct
    public void init() {
        columns = new ArrayList<String>();
        columns.add("nombre");
    }

	public String getName() {
        return name;
    }

	public List<String> getColumns() {
        return columns;
    }

	public List<Maestro> getAllMaestroes() {
        return allMaestroes;
    }

	public void setAllMaestroes(List<Maestro> allMaestroes) {
        this.allMaestroes = allMaestroes;
    }

	public String findAllMaestroes() {
        allMaestroes = maestroService.findAllMaestroes();
        dataVisible = !allMaestroes.isEmpty();
        return null;
    }

	public boolean isDataVisible() {
        return dataVisible;
    }

	public void setDataVisible(boolean dataVisible) {
        this.dataVisible = dataVisible;
    }

	public HtmlPanelGrid getCreatePanelGrid() {
        if (createPanelGrid == null) {
            createPanelGrid = populateCreatePanel();
        }
        return createPanelGrid;
    }

	public void setCreatePanelGrid(HtmlPanelGrid createPanelGrid) {
        this.createPanelGrid = createPanelGrid;
    }

	public HtmlPanelGrid getEditPanelGrid() {
        if (editPanelGrid == null) {
            editPanelGrid = populateEditPanel();
        }
        return editPanelGrid;
    }

	public void setEditPanelGrid(HtmlPanelGrid editPanelGrid) {
        this.editPanelGrid = editPanelGrid;
    }

	public HtmlPanelGrid getViewPanelGrid() {
        return populateViewPanel();
    }

	public void setViewPanelGrid(HtmlPanelGrid viewPanelGrid) {
        this.viewPanelGrid = viewPanelGrid;
    }

	public HtmlPanelGrid populateCreatePanel() {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        javax.faces.application.Application application = facesContext.getApplication();
        ExpressionFactory expressionFactory = application.getExpressionFactory();
        ELContext elContext = facesContext.getELContext();
        
        HtmlPanelGrid htmlPanelGrid = (HtmlPanelGrid) application.createComponent(HtmlPanelGrid.COMPONENT_TYPE);
        
        OutputLabel nombreCreateOutput = (OutputLabel) application.createComponent(OutputLabel.COMPONENT_TYPE);
        nombreCreateOutput.setFor("nombreCreateInput");
        nombreCreateOutput.setId("nombreCreateOutput");
        nombreCreateOutput.setValue("Nombre:");
        htmlPanelGrid.getChildren().add(nombreCreateOutput);
        
        InputText nombreCreateInput = (InputText) application.createComponent(InputText.COMPONENT_TYPE);
        nombreCreateInput.setId("nombreCreateInput");
        nombreCreateInput.setValueExpression("value", expressionFactory.createValueExpression(elContext, "#{maestroBean.maestro.nombre}", String.class));
        nombreCreateInput.setRequired(false);
        htmlPanelGrid.getChildren().add(nombreCreateInput);
        
        Message nombreCreateInputMessage = (Message) application.createComponent(Message.COMPONENT_TYPE);
        nombreCreateInputMessage.setId("nombreCreateInputMessage");
        nombreCreateInputMessage.setFor("nombreCreateInput");
        nombreCreateInputMessage.setDisplay("icon");
        htmlPanelGrid.getChildren().add(nombreCreateInputMessage);
        
        HtmlOutputText detallesCreateOutput = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        detallesCreateOutput.setId("detallesCreateOutput");
        detallesCreateOutput.setValue("Detalles:");
        htmlPanelGrid.getChildren().add(detallesCreateOutput);
        
        HtmlOutputText detallesCreateInput = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        detallesCreateInput.setId("detallesCreateInput");
        detallesCreateInput.setValue("This relationship is managed from the Detalle side");
        htmlPanelGrid.getChildren().add(detallesCreateInput);
        
        Message detallesCreateInputMessage = (Message) application.createComponent(Message.COMPONENT_TYPE);
        detallesCreateInputMessage.setId("detallesCreateInputMessage");
        detallesCreateInputMessage.setFor("detallesCreateInput");
        detallesCreateInputMessage.setDisplay("icon");
        htmlPanelGrid.getChildren().add(detallesCreateInputMessage);
        
        return htmlPanelGrid;
    }

	public HtmlPanelGrid populateEditPanel() {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        javax.faces.application.Application application = facesContext.getApplication();
        ExpressionFactory expressionFactory = application.getExpressionFactory();
        ELContext elContext = facesContext.getELContext();
        
        HtmlPanelGrid htmlPanelGrid = (HtmlPanelGrid) application.createComponent(HtmlPanelGrid.COMPONENT_TYPE);
        
        OutputLabel nombreEditOutput = (OutputLabel) application.createComponent(OutputLabel.COMPONENT_TYPE);
        nombreEditOutput.setFor("nombreEditInput");
        nombreEditOutput.setId("nombreEditOutput");
        nombreEditOutput.setValue("Nombre:");
        htmlPanelGrid.getChildren().add(nombreEditOutput);
        
        InputText nombreEditInput = (InputText) application.createComponent(InputText.COMPONENT_TYPE);
        nombreEditInput.setId("nombreEditInput");
        nombreEditInput.setValueExpression("value", expressionFactory.createValueExpression(elContext, "#{maestroBean.maestro.nombre}", String.class));
        nombreEditInput.setRequired(false);
        htmlPanelGrid.getChildren().add(nombreEditInput);
        
        Message nombreEditInputMessage = (Message) application.createComponent(Message.COMPONENT_TYPE);
        nombreEditInputMessage.setId("nombreEditInputMessage");
        nombreEditInputMessage.setFor("nombreEditInput");
        nombreEditInputMessage.setDisplay("icon");
        htmlPanelGrid.getChildren().add(nombreEditInputMessage);
        
        HtmlOutputText detallesEditOutput = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        detallesEditOutput.setId("detallesEditOutput");
        detallesEditOutput.setValue("Detalles:");
        htmlPanelGrid.getChildren().add(detallesEditOutput);
        
        HtmlOutputText detallesEditInput = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        detallesEditInput.setId("detallesEditInput");
        detallesEditInput.setValue("This relationship is managed from the Detalle side");
        htmlPanelGrid.getChildren().add(detallesEditInput);
        
        Message detallesEditInputMessage = (Message) application.createComponent(Message.COMPONENT_TYPE);
        detallesEditInputMessage.setId("detallesEditInputMessage");
        detallesEditInputMessage.setFor("detallesEditInput");
        detallesEditInputMessage.setDisplay("icon");
        htmlPanelGrid.getChildren().add(detallesEditInputMessage);
        
        return htmlPanelGrid;
    }

	public HtmlPanelGrid populateViewPanel() {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        javax.faces.application.Application application = facesContext.getApplication();
        ExpressionFactory expressionFactory = application.getExpressionFactory();
        ELContext elContext = facesContext.getELContext();
        
        HtmlPanelGrid htmlPanelGrid = (HtmlPanelGrid) application.createComponent(HtmlPanelGrid.COMPONENT_TYPE);
        
        HtmlOutputText nombreLabel = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        nombreLabel.setId("nombreLabel");
        nombreLabel.setValue("Nombre:");
        htmlPanelGrid.getChildren().add(nombreLabel);
        
        HtmlOutputText nombreValue = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        nombreValue.setId("nombreValue");
        nombreValue.setValueExpression("value", expressionFactory.createValueExpression(elContext, "#{maestroBean.maestro.nombre}", String.class));
        htmlPanelGrid.getChildren().add(nombreValue);
        
        HtmlOutputText detallesLabel = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        detallesLabel.setId("detallesLabel");
        detallesLabel.setValue("Detalles:");
        htmlPanelGrid.getChildren().add(detallesLabel);
        
        HtmlOutputText detallesValue = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        detallesValue.setId("detallesValue");
        detallesValue.setValue("This relationship is managed from the Detalle side");
        htmlPanelGrid.getChildren().add(detallesValue);
        
        return htmlPanelGrid;
    }

	public Maestro getMaestro() {
        if (maestro == null) {
            maestro = new Maestro();
        }
        return maestro;
    }

	public void setMaestro(Maestro maestro) {
        this.maestro = maestro;
    }

	public List<Detalle> getSelectedDetalles() {
        return selectedDetalles;
    }

	public void setSelectedDetalles(List<Detalle> selectedDetalles) {
        if (selectedDetalles != null) {
            maestro.setDetalles(new HashSet<Detalle>(selectedDetalles));
        }
        this.selectedDetalles = selectedDetalles;
    }

	public String onEdit() {
        if (maestro != null && maestro.getDetalles() != null) {
            selectedDetalles = new ArrayList<Detalle>(maestro.getDetalles());
        }
        return null;
    }

	public boolean isCreateDialogVisible() {
        return createDialogVisible;
    }

	public void setCreateDialogVisible(boolean createDialogVisible) {
        this.createDialogVisible = createDialogVisible;
    }

	public String displayList() {
        createDialogVisible = false;
        findAllMaestroes();
        return "maestro";
    }

	public String displayCreateDialog() {
        maestro = new Maestro();
        createDialogVisible = true;
        return "maestro";
    }

	public String persist() {
        String message = "";
        if (maestro.getId() != null) {
            maestroService.updateMaestro(maestro);
            message = "message_successfully_updated";
        } else {
            maestroService.saveMaestro(maestro);
            message = "message_successfully_created";
        }
        RequestContext context = RequestContext.getCurrentInstance();
        context.execute("createDialogWidget.hide()");
        context.execute("editDialogWidget.hide()");
        
        FacesMessage facesMessage = MessageFactory.getMessage(message, "Maestro");
        FacesContext.getCurrentInstance().addMessage(null, facesMessage);
        reset();
        return findAllMaestroes();
    }

	public String delete() {
        maestroService.deleteMaestro(maestro);
        FacesMessage facesMessage = MessageFactory.getMessage("message_successfully_deleted", "Maestro");
        FacesContext.getCurrentInstance().addMessage(null, facesMessage);
        reset();
        return findAllMaestroes();
    }

	public void reset() {
        maestro = null;
        selectedDetalles = null;
        createDialogVisible = false;
    }

	public void handleDialogClose(CloseEvent event) {
        reset();
    }

	private static final long serialVersionUID = 1L;
}
